package cn.clq.ssmshiro.dao.impl;

import cn.clq.ssmshiro.dao.ItemsDao;
import cn.clq.ssmshiro.pojo.Items;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Component
public class ItemsDaoImpl implements ItemsDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Items> getItems() {
        String sql = "select * from items";
        List<Items> list = jdbcTemplate.query(sql, new RowMapper<Items>() {
            @Override
            public Items mapRow(ResultSet rs, int rowNum) throws SQLException {
                Items items = new Items();
                items.setId(rs.getInt("id"));
                items.setName(rs.getString("name"));
                items.setCity(rs.getString("city"));
                items.setNumber(rs.getInt("number"));
                items.setPicture(rs.getString("picture"));
                items.setPrice(rs.getDouble("price"));
                return items;
            }
        });
        return list;
    }
}
